<script setup lang="ts">
import type { DefColumn } from "@/views/def/table/DefColumn";
import { queryList } from "@/api/def/columnctrl";
import { queryPage } from "@/api/def/tabledata";
import { useTable, type BasicTableProps } from "@/hooks/table";
import { onMounted, reactive, ref } from "vue";
import type { FormDesignerComponents } from "../FormComponentMixin";

const tableRef = ref(null);
const queryFormData = ref({
  tableId: ""
});
const columnList = ref<DefColumn[]>([]);

const _value = defineModel<any>();
const config = defineModel<FormDesignerComponents>("config");

onMounted(() => {
  if (config.value.props.sourceTableId) {
    queryFormData.value.tableId = config.value.props.sourceTableId;
    queryColumnDataList();
  }
  if (_value.value) {
    table.dataList = JSON.parse(_value.value);
  }
});

const queryColumnDataList = () => {
  queryList({
    tableId: config.value.props.sourceTableId,
    columnShow: true
  }).then(res => {
    if (res.code === 200) {
      columnList.value = res.data;
    }
  });
};

const exportData = () => {
  downBlobFile(
    "/admin/def/table/data/export",
    { tableId: config.value.props.sourceTableId },
    config.value.props.sourceTableComment
  );
};
const tableOptions = reactive<BasicTableProps>({
  multiple: true,
  queryForm: queryFormData.value,
  queryFunction: queryPage
});

const {
  instance: table,
  tableStyle,
  queryTableData,
  handleSortChange,
  handleResetQuery,
  handleCurrentChange,
  handleSizeChange,
  downBlobFile
} = useTable(tableOptions, tableRef);

defineExpose({
  table,
  queryTableData,
  handleResetQuery,
  exportData
});
</script>

<template>
  <div>
    <el-table
      ref="tableRef"
      v-loading="table.loading"
      border
      stripe
      :data="table.dataList"
      :cell-style="tableStyle.cellStyle"
      :header-cell-style="tableStyle.headerCellStyle"
      @sort-change="handleSortChange"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" width="50" />
      <el-table-column label="序号" type="index" width="55">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item, index) in columnList"
        :key="index"
        align="center"
        :label="item.columnComment"
        :prop="item.columnKey"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <div
            v-if="
              item.convertConfig && Object.prototype.hasOwnProperty.call(item.convertConfig, 'type')
            "
          >
            {{ row[item.convertConfig.targetColumnKey] }}
          </div>
          <div v-else>{{ row[item.columnKey] }}</div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="table.pagination.current"
      :layout="table.pagination.layout"
      :page-size="table.pagination.size"
      :total="table.pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<style scoped lang="scss"></style>
